import os
from django.shortcuts import redirect

UserData = {}
PIDs = []


def auth(AuthType="NULL", AuthNum=0):
    def outer(func):
        def inner(reqeust, *args, **kwargs):
            current_user = reqeust.COOKIES.get('user')  # cookie里面要设置user字段;通过判断是否存在cookies来确定是否已经登录。
            if not current_user:
                print("还没登录", flush=True)
                return redirect('/login')
            if AuthType == "NULL":
                return func(reqeust, *args, **kwargs)
            elif str(AuthNum) in UserData["UserAuthDict"][current_user][AuthType]:
                return func(reqeust, *args, **kwargs)
            else:
                return redirect('/InvalidAuthority')
        return inner

    return outer


mysql = {
    'ip': '192.168.201.1',
    'user': 'op',
    'passwd': 'opopop',
    'database': 'opmanager'
}

logtype = {
    "UserLog": "/var/log/Op_User.log",
    "HostLog": "/var/log/Op_host.log",
    "SysLog": "/var/log/Op_Sys.log"
}

TMP_DIR = "/tmp/Op/"

if not os.path.isdir(TMP_DIR):
    os.makedirs(TMP_DIR)

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '18#*^*n-+7m)w3ygd2a!1di8bb^*ja9vg8@!mkinih))62x&z('
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['10.17.2.172', '218.192.84.120', 'op.kratos.fun', 'cpop.kratos.fun']

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'login',
    'index',
    'HostManager',
    'AppMonitor',
    'AppManager',
    'DomainManager',
    'UserManager',
    'HostSetting',
    'Logs',
    'channels',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ASGI_APPLICATION = 'OpManager.routing.application'

ROOT_URLCONF = 'OpManager.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR + "/templates"],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'OpManager.wsgi.application'

# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = False

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_ROOT = ''
STATIC_URL = '/assets/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'assets'), ]
